package com.itic.appbase.framework.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * <p>
 * Company: itic
 * </p>
 * 
 * @author zhaokechun
 * @date 2014年8月5日 下午2:17:52
 * @version 1.0
 */
public class UploadVideoUtils {

	 public static void main(String[] args) {  
	        //VideoToVideo("C:/Users/Administrator/Desktop/ffmpeg/test.mp4", "C:/Users/Administrator/Desktop/ffmpeg/ffmpeg.exe", "C:/Users/Administrator/Desktop/ffmpeg/test.flv");  
	        
		String save = saveFileFromUrl("http://file.jhjvip.cn/images/c0213206-ff1f-40cd-801a-2fcf8e833681.mp4", "C:/Users/Administrator/Desktop/ffmpeg/"); 
		System.out.println(save);
		
		 //VideoToPicture("C:/Users/Administrator/Desktop/ffmpeg/test.mp4", "C:/Users/Administrator/Desktop/ffmpeg/ffmpeg.exe", "C:/Users/Administrator/Desktop/ffmpeg/test.png");  
	   	 
	 }  
	
	 /**  
     * 参数  
     * veido_path : 视频位置  
     * ffmpeg_path : 转换程序  
     * picPath : 图片位置  
     * */  
    public static boolean VideoToPicture(String veido_path,String ffmpeg_path, String picPath) {    
        File file = new File(veido_path);    
        if (!file.exists()) {    
            System.err.println("路径[" + veido_path + "]对应的视频文件不存在!");    
            return false;    
        }     
        List<String> commands = new java.util.ArrayList<String>();    
        commands.add(ffmpeg_path);    
        commands.add("-i");    
        commands.add(veido_path);    
        commands.add("-y");    
        commands.add("-f");    
        commands.add("image2");    
        commands.add("-ss");    
        commands.add("1");//这个参数是设置截取视频多少秒时的画面    
        //commands.add("-t");    
        //commands.add("0.001");    
        commands.add("-s");    
        commands.add("700x525");    
        commands.add(picPath);    
       try {    
            ProcessBuilder builder = new ProcessBuilder();    
            builder.command(commands);    
            builder.start();    
            System.out.println("截取成功");    
           return true;    
        } catch (Exception e) {    
           e.printStackTrace();    
           return false;    
        }    
    }   
  
    
    public static String saveFileFromUrl( String urlSrc, String saveDir ){
    	try{
    		 //new一个URL对象  
            URL url = new URL(urlSrc);  
            //打开链接  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            //设置请求方式为"GET"  
            conn.setRequestMethod("GET");  
            //超时响应时间为5秒  
            conn.setConnectTimeout(5 * 1000);  
            //通过输入流获取图片数据  
            InputStream inStream = conn.getInputStream();  
            //得到图片的二进制数据，以二进制封装得到数据，具有通用性  
            byte[] data = readInputStream(inStream);  
            String prefix=urlSrc.substring(urlSrc.lastIndexOf(".")+1);
            saveDir = saveDir+IdGen.uuid()+"."+prefix;
            File file = new File(saveDir); 
            
            //创建输出流  
            FileOutputStream outStreamTemp = new FileOutputStream(file);  
            //写入数据  
            outStreamTemp.write(data);  
            //关闭输出流  
            outStreamTemp.close();  
            
    	}catch (Exception e) {
    		e.printStackTrace();
		}
        return saveDir;
    }
    
    
    public static byte[] readInputStream(InputStream inStream) throws Exception{  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        //创建一个Buffer字符串  
        byte[] buffer = new byte[1024];  
        //每次读取的字符串长度，如果为-1，代表全部读取完毕  
        int len = 0;  
        //使用一个输入流从buffer里把数据读取出来  
        while( (len=inStream.read(buffer)) != -1 ){  
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度  
            outStream.write(buffer, 0, len);  
        }  
        //关闭输入流  
        inStream.close();  
        //把outStream里的数据写入内存  
        return outStream.toByteArray();  
    }  
    
    /**  
     * 将视频转为flv视频   
     * */  
    public static boolean VideoToVideo(String veidopath,String ffmpegpath, String codcFilePath) {   
          
        File file = new File(veidopath);    
        if (!file.exists()) {    
            System.err.println("路径[" + veidopath + "]对应的视频文件不存在!");    
            return false;    
        }     
        // 创建一个List集合来保存转换视频文件为flv格式的命令  
        List<String> commands = new java.util.ArrayList<String>();    
        commands.add(ffmpegpath); // 添加转换工具路径  
        commands.add("-i"); // 添加参数＂-i＂，该参数指定要转换的文件  
        commands.add(veidopath); // 添加要转换格式的视频文件的路径  
        commands.add("-qscale");     //指定转换的质量  
        commands.add("6");  
        commands.add("-ab");        //设置音频码率  
        commands.add("64");  
        commands.add("-ac");        //设置声道数  
        commands.add("2");  
        commands.add("-ar");        //设置声音的采样频率  
        commands.add("22050");  
        commands.add("-r");        //设置帧频  
        commands.add("24");  
        commands.add("-y"); // 添加参数＂-y＂，该参数指定将覆盖已存在的文件  
        commands.add(codcFilePath);    
       try {    
            ProcessBuilder builder = new ProcessBuilder();    
            builder.command(commands);  
            builder.redirectErrorStream(true);  
            builder.start();  
            System.out.println("转码成功！");    
           return true;    
        } catch (Exception e) {    
           e.printStackTrace();    
           return false;    
        }    
    }  
}
